import { ValueT } from '@/type';
import { WatchViewI } from '../type';
import CanvasUtils from '@/utils/CanvasUtils';
import { Color } from '@/utils/CanvasUtils/CanvasType';

/** 绘制线条参数 */
export interface LineStyle {
	/** aX位置 */
	aX: ValueT<number>;
	/** aY位置 */
	aY: ValueT<number>;
	/** bX位置 */
	bX: ValueT<number>;
	/** bY位置 */
	bY: ValueT<number>;
	/** 线条颜色 */
	color?: ValueT<Color>;
	/** 线条宽度 */
	lineWidth?: ValueT<number>;
	/** 线条两端样式 */
	lineCap?: ValueT<CanvasLineCap>;
	/** 线条连接样式 */
	lineJoin?: ValueT<CanvasLineJoin>;
	/** 线条虚线样式 */
	lineDash?: ValueT<number[]>;
	/** 透明度 */
	transparency?: ValueT<number>;
	/** 是否显示 */
	show?: ValueT<boolean>;
}

/** 线段绘制类 */
export default class LineView implements WatchViewI {

	public Z_Index: ValueT<number>;

	/** 画板类 */
	public canvas: CanvasUtils;

	/** 绘制文本参数 */
	private save: LineStyle;

	/**
	 * 构造函数
	 * @param canvas 画板类
	 * @param save 绘制线条参数
	 * @param zIndex 绘制级别(最大最上)
	 */
	constructor(canvas: CanvasUtils, save: LineStyle, zIndex?: ValueT<number>) {
		this.canvas = canvas;
		this.save = save;
		this.Z_Index = zIndex || { Value: 0 };
	}


	/** @override */
	public Draw() {
		const { save, canvas } = this;
		const { aX, aY, bX, bY, color, lineWidth, lineCap, lineJoin, lineDash, transparency, show } = save;
		if (show && !show.Value) { return; }
		canvas.Save();
		if (transparency) {
			const transparencyV = transparency.Value;
			canvas.GlobalAlpha = transparencyV;
		}
		if (color) { canvas.StrokeStyle = color.Value; }
		if (lineWidth) { canvas.LineWidth = lineWidth.Value; }
		if (lineCap) { canvas.LineCap = lineCap.Value; }
		if (lineJoin) { canvas.LineJoin = lineJoin.Value; }
		if (lineDash) { canvas.LineDash = lineDash.Value; }
		canvas.BeginPath();
		canvas.MoveTo({ X: aX.Value, Y: aY.Value });
		canvas.LineTo({ X: bX.Value, Y: bY.Value });
		canvas.Stroke();
		canvas.Restore();
	}
}
